using System;
using System.Collections.Generic;
using System.Drawing;
using Infragistics.UltraChart.Data.Series;
using MyMath;

namespace CustomChart
{
	public class MouseFigure
	{
		public double Xmin { get; set; }

		public double Xmax { get; set; }

		public double Ymin { get; set; }

		public double Ymax { get; set; }

		public double XPixel { get; set; }

		public double YPixel { get; set; }

		public List<ISeries> Series { get; set; }

		public double DistanceThreshold { get; set; }

		public MouseFigure()
		{
			Series = new List<ISeries>();
		}

		public XYSeriesEx SelectCurve(Point mouser)
		{
			XYSeriesEx result = null;
			double num = Xmax - Xmin;
			double num2 = Ymax - Ymin;
			double num3 = Xmin + num * (double)mouser.X / XPixel;
			double num4 = Ymin + num2 * (double)mouser.Y / YPixel;
			double num5 = num * DistanceThreshold / XPixel;
			double num6 = num2 * DistanceThreshold / YPixel;
			double num7 = Math.Sqrt(XPixel * XPixel + YPixel * YPixel);
			foreach (ISeries item in Series)
			{
				if (!(item is XYSeriesEx))
				{
					continue;
				}
				XYSeriesEx xYSeriesEx = item as XYSeriesEx;
				if (!xYSeriesEx.Visible)
				{
					continue;
				}
				for (int i = 0; i < xYSeriesEx.Points.Count - 1; i++)
				{
					if (!(num3 >= xYSeriesEx.Points[i].ValueX - num5) || !(num3 <= xYSeriesEx.Points[i + 1].ValueX + num5))
					{
						continue;
					}
					double num8;
					double num9;
					if (xYSeriesEx.Points[i].ValueY < xYSeriesEx.Points[i + 1].ValueY)
					{
						num8 = xYSeriesEx.Points[i].ValueY - num6;
						num9 = xYSeriesEx.Points[i + 1].ValueY + num6;
					}
					else
					{
						num8 = xYSeriesEx.Points[i + 1].ValueY - num6;
						num9 = xYSeriesEx.Points[i].ValueY + num6;
					}
					if (num4 >= num8 && num4 <= num9)
					{
						double num10 = XPixel * (xYSeriesEx.Points[i].ValueX - Xmin) / num;
						double num11 = YPixel * (xYSeriesEx.Points[i].ValueY - Ymin) / num2;
						double num12 = XPixel * (xYSeriesEx.Points[i + 1].ValueX - Xmin) / num;
						double num13 = YPixel * (xYSeriesEx.Points[i + 1].ValueY - Ymin) / num2;
						double num14 = MathCommon.Distance((double)mouser.X, (double)mouser.Y, num10, num11);
						double num15 = MathCommon.Distance((double)mouser.X, (double)mouser.Y, num12, num13);
						double num16 = MathCommon.Distance(num10, num11, num12, num13);
						double num17 = (num16 * num16 - num14 * num14 + num15 * num15) / (2.0 * num16);
						double num18 = ((!(Math.Abs(num15) - Math.Abs(num17) > 1E-05)) ? 0.0 : Math.Sqrt(num15 * num15 - num17 * num17));
						if (num18 <= DistanceThreshold && num18 < num7)
						{
							num7 = num18;
							result = xYSeriesEx;
						}
					}
					break;
				}
			}
			return result;
		}
	}
}
